package com.pinyougou.shop.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.Goods;
import com.pinyougou.service.GoodsService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@ResponseBody（将Java对象转换成json对象）+@Controller
@RestController
@RequestMapping("/goods")
public class GoodsController {

    //注入业务层的数据
    @Reference(timeout = 10000)
    private GoodsService goodsService;


    @PostMapping("/save")
    public boolean save(@RequestBody Goods goods){

        try {
            /**获取商家登录的名 */
            SecurityContext context = SecurityContextHolder.getContext();
            Authentication authentication = context.getAuthentication();
            String sellerId = authentication.getName();
            goods.setSellerId(sellerId);

            goodsService.save(goods);
            return  true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

}
